/**
 * inforcreation.com co.Ltd
 * MichaelHuang
 * 2011-12-17
 */
package com.zchat.android.receivers;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.zchat.api.connection.NetStatus;
import com.zchat.api.connection.NetUtil;
import com.zchat.api.log.Logger;
import com.zchat.api.util.ZChatSetting;

/**
 * Application boot broadcaster receiver.
 * @author MichaelHuang
 *
 */
public class ApplicationBootReceiver extends BroadcastReceiver {

	public static final String APPLICATION_BOOT = "application_boot";
	private static boolean loading = false;
	@Override
	public void onReceive(final Context context, Intent intent) {
		if(loading == true) {
			return;
		}
		loading = true;
		String action = intent.getAction();
		if(!action.equals(APPLICATION_BOOT)) {
			return;
		}
		if(ZChatSetting.DEBUG) Logger.info("ApplicationBootReceiver-Application has booted");
		//do something when receive the broadcaster, note that should start new thread when use net work.
		//1、send net status changed broadcaster
		//以下两行代码会导致某些手机自动重启，如moto xt800，切勿使用。原因未知
//		Intent netBc = new Intent("android.net.conn.CONNECTIVITY_CHANGE");
//		context.sendBroadcast(netBc);
		//默认系统启动时，网络为可用状态，是否真正可用则由ping值决定 
		NetUtil.getInstance().setNetStatus(context, NetStatus.avaliable);
	}
	
	public void register(Application application)
	{
		IntentFilter applicationBootFilter = new IntentFilter(APPLICATION_BOOT);
		application.registerReceiver(this, applicationBootFilter);
	}

}
